package com.le.dub.controller;

import cn.hutool.core.net.multipart.UploadFile;
import cn.hutool.core.util.ObjectUtil;
import com.le.dub.common.DateUtils;
import com.le.dub.conf.UserUtil;
import com.le.dub.mobile.User;
import com.le.dub.mobile.bo.AddDubRoleBO;
import com.le.dub.mobile.bo.DubRoleListBO;
import com.le.dub.mobile.vo.DubRoleVO;
import com.le.dub.service.IDubRoleService;
import com.le.dub.mobile.DubRole;
import com.le.dub.common.Response;
import com.le.dub.service.IFileService;
import com.le.dub.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/app/dubRole")
public class AppDubRoleController {
    @Resource
    public IDubRoleService dubRoleService;
    @Resource
    public IFileService fileService;
    @Resource
    public IUserService userService;

    /**
     * 创建角色
     *
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value = "/createDubRole")
    public Response createDubRole(HttpServletRequest request, @RequestParam("headFile") MultipartFile headFile, @RequestParam("musicFile") MultipartFile musicFile,
                                  @RequestParam("roleName") String roleName) throws Exception {

        String headUrl = fileService.upload(headFile, 1);
        String musicUrl = fileService.upload(musicFile, 1);
        AddDubRoleBO addDubRoleBO = new AddDubRoleBO();
        Long userId = UserUtil.getUserId(request);
        addDubRoleBO.setUserId(userId);
        addDubRoleBO.setRoleName(roleName);
        addDubRoleBO.setHeadUrl(headUrl);
        addDubRoleBO.setSoundUrl(musicUrl);
       Long roleId= dubRoleService.createDubRole(addDubRoleBO);

        return Response.success(roleId);

    }

    /**
     * 删除信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}")
    public Response deleteDubRole(@PathVariable("id") Long id) {

        dubRoleService.removeById(id);
        return Response.success();

    }

    /**
     * 根据id查询对象
     *
     * @return
     */

    @RequestMapping(value = "find/{id}")
    public Response getDubRole(@PathVariable("id") Long id) {


        return Response.success(dubRoleService.getById(id));
    }


    /**
     * 获取市场声音列表
     *
     * @return
     */


    @RequestMapping(value = "/dubRoleList")
    public Response dubRoleList(HttpServletRequest request,@RequestBody  DubRoleListBO dubRoleListBO) {
        Long userId = UserUtil.getUserId(request);
        if(ObjectUtil.isNull(userId)){
            userId=0L;
        }
        dubRoleListBO.setUserId(userId);
        List<DubRoleVO> dubRoleList=dubRoleService.dubRoleList(dubRoleListBO);

        for (DubRoleVO vo : dubRoleList) {
            vo.setCreateTimeFormat(DateUtils.format(vo.getCreateTime()));
        }
        return Response.success(dubRoleList);
    }


    /**
     * 获取我的声音列表
     *
     * @return
     */


    @RequestMapping(value = "/dubUserRoleList")
    public Response dubUserRoleList(HttpServletRequest request,@RequestBody  DubRoleListBO dubRoleListBO) {
        Long userId = UserUtil.getUserId(request);
        dubRoleListBO.setUserId(userId);
        User user=userService.getById(userId);
        List<DubRoleVO> dubUserRoleList=dubRoleService.dubUserRoleList(dubRoleListBO);
        for (DubRoleVO vo : dubUserRoleList) {
            vo.setCreateTimeFormat(DateUtils.format(vo.getCreateTime()));
            vo.setNick(user.getNick());
        }
        return Response.success(dubUserRoleList);
    }

    @GetMapping(value = "/cloneRecordListByUser")
    public Response cloneRecordListByUser(HttpServletRequest request) {
        Long userId = UserUtil.getUserId(request);
        List<DubRole> roleList=dubRoleService.cloneRecordListByUser(userId);
        return Response.success(roleList);
    }
    @GetMapping(value = "/roleById")
    public Response roleById(@RequestParam("roleId") Long roleId) {

        return Response.success(dubRoleService.getById(roleId));
    }

}